<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Model\CloseFriend;
use App\Model\CloseFriendLog;
use App\Model\UserProfile;
use Encore\Admin\Grid;
use Encore\Admin\Controllers\ModelForm;
use Encore\Admin\Grid\Displayers\Actions;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\Table;

/**
 * 亲密值
 */
class CloseFriendController extends Controller
{
    use ModelForm;

    public function index()
    {
        $content = new Content();
        $content->header('亲密值记录');
        $content->body($this->grid());
        return $content;
    }

    protected function grid()
    {
        $grid = new Grid(new CloseFriend());
        $where = [];
        $grid->model()->where($where)->orderBy('close_value', 'desc');
        $grid->userProfile()->user_profile_id('用户ID');
        $grid->userInfo()->cur_channel('用户渠道');
        $grid->userProfile()->nick_name('用户昵称');
        $grid->userProfile()->role('用户角色')->display(function ($data) {
            return $data == UserProfile::ROLE_USER ? '用户' : '女用户';
        });
        $grid->frdProfile()->user_profile_id('好友ID');
        $grid->frdInfo()->cur_channel('好友渠道');
        $grid->frdProfile()->nick_name('好友昵称');
        $grid->frdProfile()->role('好友角色')->display(function ($data) {
            return $data == UserProfile::ROLE_USER ? '用户' : '女用户';
        });
        $grid->close_value('亲密值')->modal('亲密值记录', function ($model) {
            $data = CloseFriendLog::getLogs($this->user_id, $this->frd_user_id);
            foreach ($data as &$item) $item['created_at'] = date('Y-m-d H:i:s', strtotime($item['created_at']));
            return new Table(['亲密值', '记录时间'], $data);
        });
        $grid->consume_coin('消费金币');
        $grid->created_at('成为密友时间')->display(function ($time) {
            return !$time ? '-' : date('Y-m-d H:i:s', strtotime($time));
        });
        $grid->disableExport();
        $grid->disableRowSelector();
        $grid->filter(function ($filter) {
            $filter->expand();
            $filter->disableIdFilter();
            $filter->column(1 / 3, function ($filter) {
                $filter->equal('userProfile.user_profile_id', '用户ID');
            });
            $filter->column(1 / 3, function ($filter) {
                $filter->equal('frdProfile.user_profile_id', '密友ID');
            });
        });
        $grid->disableActions();
        $grid->actions(function (Actions $actions) {
            $actions->disableDelete();
            $actions->disableView();
            $actions->disableEdit();
        });
        return $grid;
    }
}
